package com.jims.master.modules.csm.service; /**
 * jims
 */

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.modules.csm.dao.ExpClassDictDao;
import com.jims.master.modules.csm.entity.ExpClassDict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * 消耗品类别字典Service
 *
 * @author fengyg
 * @version 2017-05-04
 */
@Service
@Transactional(readOnly = true)
public class ExpClassDictService extends CrudService<ExpClassDictDao, ExpClassDict> {

    public ExpClassDict get(String id) {
        return super.get(id);
    }

    public List<ExpClassDict> findList(ExpClassDict expClassDict) {
        return super.findList(expClassDict);
    }

    public Page<ExpClassDict> findPage(Page<ExpClassDict> page, ExpClassDict expClassDict) {
        return super.findPage(page, expClassDict);
    }

    @Transactional(readOnly = false)
    public int save(ExpClassDict expClassDict) {
        return super.save(expClassDict);
    }

    @Transactional(readOnly = false)
    public int delete(ExpClassDict expClassDict) {
        return super.delete(expClassDict);
    }

    /**
     * 验证消耗品类别唯一性
     *
     * @param expClassDict
     * @return
     */
    public int validUnique(ExpClassDict expClassDict) {
        if (expClassDict.getParent() != null && expClassDict.getParent().getId() != null && !expClassDict.getParent().getId().equals("")) {
            ExpClassDict dict = super.get(expClassDict.getParent().getId());
            expClassDict.setClassCode(dict.getClassCode() + expClassDict.getClassCode());
        }
        List<ExpClassDict> list = dao.validUnique(expClassDict);
        return list.size();
    }

    /**
     * 根据父级ID查询
     *
     * @param parentId
     * @return
     */
    public int findByParentId(String parentId) {
        return dao.findByParentId(parentId);
    }

    @Transactional(readOnly = false)
    public int saveClassDict(ExpClassDict expClassDict) {
        if (expClassDict.getParent() != null && expClassDict.getParent().getId() != null && !expClassDict.getParent().getId().equals("")) {
            ExpClassDict dict = super.get(expClassDict.getParent().getId());
            expClassDict.setClassCode(dict.getClassCode() + expClassDict.getClassCode());
        }
        return super.save(expClassDict);
    }
}